<?php
if (get_the_title($post->ID)=='Certificate Classes') {
// Custom Template for Single Posts in Certificate Classes
include 'single-cert-classes.php';
} else {

global $options;
foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
$dateformat = get_option('date_format');
$timeformat = get_option('time_format');
$layout = get_post_meta($post->ID, 'template', true);
get_header(); 
?>

<?php 
//set variables for use in template
$cat_ID = get_cat_ID('Classes');
$cat_link = get_category_link($cat_ID)
?>



  <div id="content">
    <div class="wrap">
    
      <?php wp_reset_query(); 
      if (have_posts()) : while (have_posts()) : the_post(); ?>
	  
      <?php if ($layout != 'full') { ?>
      <div class="column column-narrow">
      
      	<?php 
      	$currCat = get_the_category();
      	$current = $currCat[0]->name;
      	$parent = get_cat_name($currCat[0]->category_parent);
      	
    	echo "<p class='heading'>".$parent."</p>";
    	
		/*if ($current !== 'Classes') {
		list_cat_posts_all();
		}*/
		
		list_cat_posts_all();
		
		?>
      
        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar: Posts (Left)') ) : ?>&nbsp;<?php endif; ?>
      
      </div><!-- end .column-narrow -->
      <?php } ?>
      
      <div class="column <?php if ($layout == 'double') {echo"column-double column-last ";} if ($layout == 'full') {echo"column-full column-last ";} ?>column-content column-double">
	  	<div id="crumbs">
			<!--?php the_breadcrumb(); ?-->
			<?php
				$args = array(
					'prefix' => '<p>',
					'suffix' => '</p>',
					'title' => __( '', 'breadcrumbs-plus' ),
					'home' => __( 'Home', 'breadcrumbs-plus' ),
					'sep' => '»',
					'front_page' => false,
					'bold' => true,
					'show_blog' => false,
					'echo' => true,
					'singular_post_taxonomy' => 'category',
				);
				breadcrumbs_plus( $args );
			?>
		</div>
		<h1><?php the_title(); ?></h1>
        
        <?php
       //permanent course information, not conditional
       $perm_class_info= get_group('Permanent Course Information'); // use the Custom Group name
        if (!is_null($perm_class_info)) {
			foreach($perm_class_info as $perm_info){
			
				//assign each field a variable name
				$refNumber = $perm_info['permanent_info_reference_number'][1];
				$courseNumber = $perm_info['permanent_info_course_number'][1];
				$courseDescription = $perm_info['permanent_info_catalogue_description'][1];
				$lastOff = get('currently_offered_detail_last_offered');
				$nextOff = get('currently_offered_detail_next_offered');
				
				if ( stristr( $lastOff, 'new' ) ) { $lastOff = '<em>New Course</em>'; }
				
				if (strlen($courseDescription)>0) { echo emptyP( $courseDescription ); } 

			//end foreach permclassinfo as perminfo
			}  
		
		//end if not null permclassinfo
		}
        ?>
       
       <?php
       //conditional block
	   //declare global semester dates set in class-related-functions.php
		global 
		$start_spring,$end_spring,
		$start_summerA,$end_summerA,
		$start_summerB,$end_summerB,
		$start_summerC,$end_summerC,
		$start_fall,$end_fall;
		
		$ts_current = strtotime(date('j F Y'));
		
		$all_semesters = array( 'spring', 'summer A', 'summer B', 'summer C', 'fall' );

		foreach ( $all_semesters as $semester) {
			$form_semester = str_replace( ' ', '', $semester );
			$start = strtotime( ${'start_'.$form_semester } );
			$end = strtotime( ${'end_'.$form_semester } );
			if ( $start <= $ts_current && $end >= $ts_current ) {
				$all[] = strtolower($semester).' '.date('Y');
			}
		}

       $current_classes = get_group('Currently Offered Details'); // use the Custom Group name
        if (!is_null($current_classes)) {
			
			foreach($current_classes as $current_class){
			$offered = $current_class['currently_offered_detail_semester'][1];
			
			//find out which semester the class details apply to
			//hides outdated information and information entered ahead of time for future semesters
			if ( strlen( $offered ) > 0 ) {
				//show details only if they apply to current semester(haven't expired)
				//(compares today's date to array of current semesters prepared above ($all))
				if ( in_array( strtolower($offered), $all ) || stristr( $offered, 'special' ) ) {
					//assign each field a variable name							
					$classDays = $current_class['currently_offered_detail_days'][1];
					$classTime = $current_class['currently_offered_detail_time'][1];
					$classCampus = $current_class['currently_offered_detail_campus'][1];
					$classRoom = $current_class['currently_offered_detail_room'][1];
					$classLink = $current_class['currently_offered_detail_link_to_syllabus'][1];
					
					if ( stristr( $offered, 'special' ) ){
					$offered = 'Special Course';
					$special = true;
					$days = '';
					$special_course = get_group('Special Course Details'); // use the Custom Group name
						foreach( $special_course as $detail ){
							foreach ( $detail as $dates ){
								sort( $dates, SORT_NUMERIC );
								foreach ( $dates as $date ){
									$days .= $date.", ";
								}
							}
						}
					}
					//displays all days if there is at least one day selected
					else if (!is_null( $classDays ) && ! $days ) {
						foreach ($classDays as $classDay) {
							if (strlen($classDay)>0) { 
								//converts Tue to Tuesday, etc. and saves it over old classDay
								$classDay = date( 'l', strtotime( $classDay ) );
								$days .= $classDay.", "; 
							}
						} 
					}
			$days = rtrim( $days, ', ' );
			
			$profDetails = get_group('Instructor Details');
			if ( $profDetails ) {
				foreach ( $profDetails as $profDetail) {
				//assign variables only if current instructor
					if ( $profDetail['instructor_detail_current_instructor'][1] && $profDetail['instructor_detail_name'][1] ) {
						$profPost_ID = $profDetail['instructor_detail_name'][1];
						$profPost = get_post( $profPost_ID );
						$profName = $profPost->post_title;
						$staff = get('general_detail_if_staff', 1, 1, true, $profPost_ID );
					   if ( $staff == "Yes" ) {
						   $staff_id = get_cat_ID( 'Staff' );
						   $staff_link = get_category_link( $staff_id );
						   $link_back  = add_query_arg( 'staff-member', $profPost->post_name , $staff_link ); 
						   $profName = "<a class='link-staff' href='".$link_back."'>".$profName."</a>";
						}
						$profEmail = get('contact_detail_email', 1, 1, true, $profPost_ID );
						$profRoom = get('contact_detail_building_room_number', 1, 1, true, $profPost_ID );
						$profHours = $profDetail['instructor_detail_office_hours'][1];			
						
						$profs[$profName] = array( $profEmail, $profRoom, $profHours );
					}
				}
			}
			?>
            	<h6>
            		<?php 
            			echo $offered.' Details ';
            			if ( in_array( strtolower($offered), $all )) {
							echo '<em id="current-class">Current Class</em>';
						}
            		?>
            	</h6>
                <dl class="tablelike">
                	<?php if ( $refNumber ) { ?>
                		<dt>Ref #:</dt>
                    	<dd><?php echo $refNumber; ?></dd>
                	<?php } ?>
                	<dt>Course ID:</dt>
                    <dd><?php echo $courseNumber; ?></dd>
                	<dt><?php if ( $special ) { ?>Date(s):<?php } else { ?>Days:<?php } ?></dt>
                    <dd><?php echo $days; ?></dd>
                	<dt>Time:</dt>
                    <dd><?php echo $classTime; ?></dd>
                	<dt>Campus:</dt>
                    <dd><?php  echo $classCampus; ?></dd>
                	<dt>Room:</dt>
                    <dd><?php echo $classRoom; ?></dd>
                </dl>

                <?php
				if ( $profs ) {
				echo "<h6>Instructor Details</h6>";
				$x = 0;
					foreach ( $profs as $prof=>$info ){
						echo "
						<dl class='tablelike'>
							<dt>Name:</dt>
							<dd>".$prof."</dd>";
							if ($info[0]) { echo "<dt>Email:</dt><dd><a href='mailto:".$info[0]."?subject=[ GIS Web Inquiry ] ".$refNumber.": ".get_the_title()."'>".$info[0]."</a></dd>"; }
							if ($info[1]) { echo "<dt>Office:</dt><dd>".$info[1]."</dd>"; }
							if ($info[2]) { echo "<dt>Hours:</dt><dd>".$info[2]."</dd>"; }
							echo "</dl>";
						$x++;
						//if ( ! $x & 1 ) { echo "<div class='clear'>&nbsp;</div>"; }
					}
					//if ( $x & 1 ) { echo "<div class='clear'>&nbsp;</div>"; }
				}
				?>  
                    <?php if ( url_exists( $classLink ) ) { ?>
                    <p class="action-button"><a href='<?php echo $classLink; ?>' target='_blank'><?php if ( $special ) { echo "Learn more..."; } else { echo "View Course Syllabus"; }?></a></p>
					<?php } ?>
            <?php	 	
					//end current semester details condition
					//everything that should disappear when the semester ends should go above this closing bracket
					}
					else { ?>
            			<h6>Course Details</h6>
                        <dl class="tablelike">
		                	<?php if ( $refNumber ) { ?>
		                		<dt>Ref #:</dt>
		                    	<dd><?php echo $refNumber; ?></dd>
		                	<?php } ?>
                            <dt>Course ID:</dt>
                            <dd><?php echo $courseNumber; ?></dd>
                            <dt>Last Offered:</dt>
                            <dd><?php echo $lastOff; ?></dd>
                            <dt>Next Offered:</dt>
                            <dd><?php echo $nextOff; ?></dd>
                        </dl>
                    <?php
                    }
				//end if strlen offered > 0
				}
				
			//end foreach currentclasses as currentclass
			}  
		
		//end if not null currentclasses
		}
        ?>
        
        <p class="category"><a href='<?php echo $cat_link; ?>'>Show all Classes</a></p>
        
        <?php /* wp_link_pages(array('before' => '<p class="pages"><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); */ ?>
        <?php /* the_tags( '<p class="tags"><strong>Tags:</strong> ', ', ', '</p>'); */ ?>
      
      
      </div><!-- end .column-content -->

      <?php if ($layout != 'double' && $layout != 'full') { ?><div class="column column-narrow column-last">
      
        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar: Posts (Right)') ) : ?>&nbsp;<?php endif; ?>
      
      </div><!-- end .column-narrow -->
      <?php } ?>

<?php endwhile; endif; ?>

      <div class="clear">&nbsp;</div>

    </div><!-- end .wrap -->
  </div><!-- end #content -->

<?php get_footer(); ?>
<?php } ?>